package com.builder.inputeditor;

import com.builder.inputeditor.fieldtypefields.*;
import com.common.layoutmanager.columnlayout.Alignment;
import com.common.layoutmanager.columnlayout.ColumnLayout;
import com.common.layoutmanager.columnlayout.commands.NewRow;
import com.common.layoutmanager.columnlayout.commands.NewSection;
import com.gooddies.swing.hComboBox;
import java.awt.Dimension;
import java.awt.event.*;
import java.util.List;
import javax.swing.*;

/**
 * @author Dmitry
 */
public class InputFieldPanel extends JPanel {

    private final JTextField fieldName;
    private hComboBox<FieldType> fieldType;
    private final JPanel paramsPanelHolder;
    private AbstractFieldParams fieldParams;
    private final List<InputFieldPanel> allParams;
    private final InputEditorDialog parent;

    public InputFieldPanel(List<InputFieldPanel> allParams, InputEditorDialog parent) {
        this.parent = parent;
        this.allParams = allParams;
        setLayout(new ColumnLayout(Alignment.LEFT_TOP, 5, 5).setWholePanelAlignment(Alignment.LEFT));
        add(new JLabel("Field name"));
        fieldName = new JTextField();
        fieldName.setPreferredSize(new Dimension(120, 20));
        fieldName.setText("InputField" + allParams.size());
        add(fieldName);
        add(new NewRow());
        initFieldType();
        add(new JLabel("Field type"));
        add(fieldType);
        add(new NewSection());
        paramsPanelHolder = new JPanel();
        paramsPanelHolder.setPreferredSize(new Dimension(150, 50));
        paramsPanelHolder.setLayout(new ColumnLayout(Alignment.LEFT_TOP).setStartYOffset(0));
        addMouseListener(getMouseListener());
        add(paramsPanelHolder);
        fieldParams = new StringFieldParams();
        fieldParams.setParent(this);
    }

    private MouseListener getMouseListener() {
        return new MouseAdapter() {
            @Override
            public void mouseReleased(MouseEvent e) {
                if (e.isPopupTrigger()) {
                    popup(e.getX(), e.getY(), (JComponent) e.getComponent());
                }
            }
        };
    }

    private void popup(int x, int y, JComponent c) {
        JPopupMenu menu = new JPopupMenu();
        JMenuItem removeItem = new JMenuItem("Remove");
        removeItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                removeAction();
            }
        });

        JMenuItem addItem = new JMenuItem("Add");
        addItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                parent.onAddNewField();
            }
        });

        JMenuItem reorderItem = new JMenuItem("Reorder");
        reorderItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                parent.reorder();
            }
        });

        menu.add(addItem);
        menu.add(removeItem);
        menu.add(reorderItem);
        menu.show(c, x, y);
    }

    private void removeAction() {
        parent.removeField(this);
    }

    public String getFieldName() {
        return fieldName.getText();
    }

    public AbstractFieldParams getFieldParams() {
        return fieldParams;
    }

    private void initFieldType() {
        fieldType = new hComboBox<>(FieldType.values());
        fieldType.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                FieldType type = fieldType.getSelectedItem();
                switch (type) {
                    case Long:
                        fieldParams = new LongFieldParams();
                        break;
                    case String:
                        fieldParams = new StringFieldParams();
                        break;
                    case Timestamp:
                        fieldParams = new TimestampFieldParams();
                        break;
                }

                fieldParams.setParent(InputFieldPanel.this);
                fieldParams.init(paramsPanelHolder, allParams);
                paramsPanelHolder.revalidate();
                paramsPanelHolder.validate();
                paramsPanelHolder.repaint();
            }
        });
    }

    @Override
    public String toString() {
        return getFieldName();
    }
}
